package top.vanchor.myblog2back;

import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.vanchor.myblog2back.config.JwtConfigProperties;
import top.vanchor.myblog2back.utils.JWTUtils;
import top.vanchor.myblog2back.utils.JwtInterceptor;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by IntelliJ IDEA.
 *
 * @Author : Ding
 * @create 2021/11/10 13:29
 */
@CrossOrigin
@RestController
@RequestMapping("/user")
public class demo {

    @Autowired
    private JwtConfigProperties jwtConfigProperties;


    @PostMapping("/login")
    @ResponseBody
    public JSONObject login(@RequestBody JSONObject data){
        String name = String.valueOf(data.get("name"));
        JSONObject jsonObject = new JSONObject();
        if (!name.equals("")){
            // 登录成功，返回token
            jsonObject.put("token", JWTUtils.sign("1",jwtConfigProperties));
        }else {
            jsonObject.put("token", "error");
        }
        return jsonObject;
    }

    @GetMapping("/unlogin")
    @ResponseBody
    public JSONObject unlogin(String msg){
        String ret = "";
        switch (msg){
            case "1":
                ret = "未登录，请先登录";
                break;
            case "2":
                ret = "用户状态失效，请重新登陆";
                break;
            default:
                ret = msg;
                break;
        }
        System.err.println(ret + ".." + msg);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code",300);
        jsonObject.put("msg",ret);
        return jsonObject;
    }
//
//
//    @RequestMapping("/get")
//    @ResponseBody
//    public JSONObject get(){
//        JSONObject jsonObject = new JSONObject();
//        jsonObject.put("code", 200);
//        jsonObject.put("msg", Integer.parseInt(JWTUtils.getUserId(JwtInterceptor.getTokenInfo())));
//        return jsonObject;
//    }


}
